package cate.game.activity.cutstone;

import cate.common.table.activity.cutstone.row.CutRewardPoolRow;
import cate.common.table.activity.cutstone.row.CutStoneBaseRow;
import cate.common.table.activity.cutstone.row.LevelRandomRow;
import cate.common.table.activity.cutstone.row.RewardNumRow;
import cate.common.table.d.GDOperation;
import cate.common.table.d.GDWarOrder;
import cate.common.util.GameResult;
import cate.common.util.random.RWList;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.cutstone.msg.CutStoneInfoResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.task.TaskGroup;
import easy.java.dev.note.NoteField;

import java.util.List;

public class CutStoneActivity extends RoleActivitySingle {
    @NoteField("是否放了石头")
    public boolean put;
    @NoteField("石头等级")
    public int level;
    @NoteField("奖励")
    public MixRes reward;
    @NoteField("已切石次数")
    public int cutTimes;


    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (reward == null) {
            reward = new MixRes();
        }
    }

    public GameResult<CutStoneActivity> putStone() {
        GameResult<CutStoneActivity> r = new GameResult<>();
        r.data = this;
        if (put) {
            return r.fail("一次只能切一块石头哦");
        }
        CutStoneBaseRow baseRow = findBaseRow();
        if (baseRow == null) {
            return r.fail("基础配置错误");
        }
        MixRes cost = new MixRes(baseRow.putCost);
        GameResult<MixResItem> result = cost.consume(role);
        if (!result.ok()) {
            return r.fail(result);
        }
        put = true;
        return r;
    }

    public GameResult<CutStoneActivity> cutStone() {
        GameResult<CutStoneActivity> r = new GameResult<>();
        r.data = this;
        if (!put) {
            return r.fail("请先放入石头");
        }
        CutStoneBaseRow baseRow = findBaseRow();
        if (baseRow == null) {
            return r.fail("基础配置错误");
        }

        List<LevelRandomRow> list = role.getGame().table.cutStone.levelRandom.getListByStartQuality(this.level);
        if (list == null || list.isEmpty()) {
            return r.fail("品质已达最高");
        }
        RWList<LevelRandomRow> pool = new RWList<>();
        pool.addAndSetup(list);
        LevelRandomRow row = pool.get();
        if (row == null) {
            return r.fail("鉴宝配置错误");
        }
        RewardNumRow quality = role.getGame().table.cutStone.rewardNum.getRowByQuality(row.targetQuality);
        if (quality == null) {
            return r.fail("奖励数量配置错误");
        }
        List<CutRewardPoolRow> rewardRows = role.getGame().table.cutStone.cutRewardPool.getListByQuality(row.targetQuality);
        if (rewardRows == null || rewardRows.isEmpty()) {
            return r.fail("奖池配置错误");
        }
        MixRes cost = new MixRes(baseRow.cutCost);
        GameResult<MixResItem> result = cost.consume(role);
        if (!result.ok()) {
            return r.fail(result);
        }
        this.cutTimes++;
        if (this.cutTimes >= baseRow.cheat) {
            int q = role.getGame().table.cutStone.levelRandom.getBestQuality();
            if (q <= 0) {
                return r.fail("品质配置错误");
            }
            this.level = q;
            this.cutTimes = 0;
            rewardRows = role.getGame().table.cutStone.cutRewardPool.getListByQuality(this.level);
        }else {
            this.level = row.targetQuality;
        }
        this.reward.clear();
        for (int i = 0; i < quality.num; i++) {
            RWList<CutRewardPoolRow> rewardPool = new RWList<>();
            rewardPool.addAndSetup(rewardRows);
            CutRewardPoolRow rewardRow = rewardPool.get();
            this.reward.addList(new MixRes(rewardRow.reward));
            rewardRows.remove(rewardRow);
        }

        return r;
    }

    public GameResult<CutStoneActivity> takeReward() {
        GameResult<CutStoneActivity> r = new GameResult<>();
        r.data = this;
        if (!put) {
            return r.fail("请先放入石头");
        }
        if (this.reward.isEmpty()) {
            return r.fail("没有奖励");
        }
        this.reward.addAndDialog(role, true, GDOperation.CUT_STONE);
        this.reward.clear();
        this.put = false;
        this.level = 0;
        this.cutTimes = 0;
        return r;
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        this.reward.clear();
        this.put = false;
        this.level = 0;
        role.getTask().removeGroup(funcId());
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    public void noticeUpdate() {
        role.sendNow(new CutStoneInfoResp(this));
    }

    @Override
    public void excelCheck() {
        List<Integer> taskIds = role.getGame().table.cutStone.cutTask.getTaskIds(configTid());
        role.getTask().getGroup(funcId()).addItemListNoRepeat(taskIds, false);
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        refreshDailyTask(silence);
    }

    private void refreshDailyTask(boolean silence) {
        List<Integer> taskIds = role.getGame().table.cutStone.cutTask.getTaskIds(configTid(), GDWarOrder.TaskType.DAILY);
        TaskGroup taskGroup = role.getTask().getGroup(funcId());
        taskGroup.removeItemList(taskIds);
        taskGroup.addItemListNoRepeat(taskIds, silence);
    }

    private CutStoneBaseRow findBaseRow() {
        return role.getGame().table.cutStone.base.get(configTid());
    }
}
